package com.ren.mianshi.config;


import com.ren.mianshi.config.annotion.Config;
import com.ren.mianshi.config.annotion.PRE;
import com.ren.mianshi.config.annotion.SC;
import com.ren.mianshi.config.annotion.TEST;

/**
 * @author renheng
 * @Description
 * @date 2020/9/22
 */

public class ConfigUtil {


    /**
     * 获取环境
     * @param clazz
     * @return
     */
    public static HostEnvironment getEnvironment(Class clazz){
        Config config = (Config) clazz.getAnnotation(Config.class);
        return config.environment();
    }

    /**
     * 根据环境获取参数
     * @param clazz
     * @param fieldName
     * @return
     */
    public static String getConfig(Class clazz,String fieldName){

        HostEnvironment environment = getEnvironment(clazz);

        try {
            if (environment== HostEnvironment.PRE){
                PRE pre = clazz.getDeclaredField(fieldName).getAnnotation(PRE.class);
                return pre.value();
            }else if (environment== HostEnvironment.TEST){
                TEST test = clazz.getDeclaredField(fieldName).getAnnotation(TEST.class);
                return test.value();
            }else {
                SC sc = clazz.getDeclaredField(fieldName).getAnnotation(SC.class);
                return sc.value();
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }




}
